package com.tju.backend.resources.config.initial;

import com.tju.backend.plugin.common.GlobalCall;
import com.tju.backend.plugin.t_io_server.SPServerAioHandler;
import com.tju.backend.plugin.t_io_server.SPServerAioListener;
import com.tju.backend.plugin.web_socket.TioWebsocketStarter;
import com.tju.backend.utils.tool.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.tio.server.ServerTioConfig;
import org.tio.server.TioServer;
import org.tio.server.intf.ServerAioHandler;
import org.tio.server.intf.ServerAioListener;

/**
 * 初始化通讯
 *
 * @USER: CLS
 */
@Slf4j
@Component
public class NewsletterInitial implements CommandLineRunner {

    @Value("${tio.core.server.port}")
    private int port_01;

    @Override
    public void run(String... args) {
        try {
            log.info("初始化通讯.....");
            //初始化WebSocket
            TioWebsocketStarter.start();

            //初始化TCP/IP服务端
            ServerAioHandler aioHandler = new SPServerAioHandler();
            ServerAioListener aioListener = new SPServerAioListener();
            ServerTioConfig serverTioConfig = new ServerTioConfig(aioHandler, aioListener);
            TioServer tioServer = new TioServer(serverTioConfig); //可以为空
            //启动服务端
            tioServer.start(null, port_01);

            log.info("初始化通讯完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
